package com.sqfw.project.wp.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.common.enums.knowledge.KlFileTypeEnum;
import com.sqfw.project.knowledge.domain.KlContentFileInfo;
import com.sqfw.project.utils.TikaFileValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wp.domain.SysMiniFile;
import com.sqfw.project.wp.service.ISysMiniFileService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件表Controller
 *
 * @author lx
 * @date 2023-06-07
 */
@Slf4j
@Api(tags = "文件接口")
@RestController
@RequestMapping("/wp/sys_mini_file")
public class SysMiniFileController extends BaseController {
    @Autowired
    private ISysMiniFileService sysMiniFileService;

    /** 文件大小限制 */
    private static final long THREE_B = 3 * 1024 * 1024;

    /**
     * 文件上传，成功返回文件id
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "文件上传", consumes = "application/text",
            httpMethod = "POST", response = Object.class)
    @PreAuthorize("@ss.hasPermi('wp:sys_mini_file:uploadFile')")
    @PostMapping("/uploadFile")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file, String fileType) throws IOException {
        if (fileType == null) {
            fileType = "zxsl";
        }
        if (file.isEmpty()) {
            return AjaxResult.error("上传失败，请选择文件!");
        }
        String originalFilename = file.getOriginalFilename();
        // 文件后缀
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 附件格式：png、jpg、doc、docx、txt、pdf
       List<String> fileExtensions = new ArrayList<>();
        fileExtensions.add(".jpg");
        fileExtensions.add(".png");
        fileExtensions.add(".pdf");
        fileExtensions.add(".doc");
        fileExtensions.add(".docx");
        fileExtensions.add(".txt");
        fileExtensions.add(".xlsx");
        fileExtensions.add(".xls");
        if (!fileExtensions.contains(fileExtension)){
            return AjaxResult.error("上传文件格式不对：" + fileExtension);
        }
        long size = file.getSize() / 1024 / 1024;
        //上传文件大小不能超过3M
        if (size >= THREE_B) {
            return AjaxResult.error("上传失败，文件大小超过3M!");
        }
        boolean bool = TikaFileValidator.validateFileExtension(file);
        if (!bool) {
            //文件类型和文件内容不匹配
            return AjaxResult.error("文件类型和文件内容不匹配！");
        }
        String id = sysMiniFileService.uploadFile(file, fileType);
        if (id == null) {
            return AjaxResult.error("上传失败！");
        }
        return AjaxResult.success("上传成功！", id);
    }

}
